package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kindergarten.pojo.Role;
import com.kindergarten.pojo.User;
import com.kindergarten.pojo.UserRole;
import com.kindergarten.service.IRolePageService;
import com.kindergarten.service.IRoleService;
import com.kindergarten.service.IUserRoleService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-11-02
 */
@Controller
@RequestMapping("/role-api")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @Autowired
    IUserRoleService userRoleService;

    @Resource
    IRolePageService iRolePageService;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("topage")
    public String topage(){
        return "role";
    }

    /**
     * 左侧角色菜单
     * @return
     */
    @ResponseBody
    @PostMapping("getAllRole")
    public List<Role> getAllRole(){
       return roleService.list();
    }

    /**
     * 移除用户角色
     * @return
     */
    @ResponseBody
    @PostMapping("delUserRoleByUserIdAndRoleId")
    public DataResults delUserRoleByUserIdAndRoleId(String userId, Integer roleId, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user.getUserId().equals(userId)){
            return DataResults.success(ResultCode.N0_DELETE_SELF);
        }else{
            userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id",userId).eq("role_id",roleId));
            return DataResults.success(ResultCode.SUCCESS);
        }
    }

    /**
     *  打开选择用户页面
     * @return
     */
    @RequestMapping("selectUserRole")
    public String selectUserRole(){
        return "selectUserRole";
    }

    /**
     * Method name: addUserRole <BR>
     * Description: 增加用户的角色 <BR>
     * @param roleId
     * @param userIds
     * @return String<BR>
     */
    @ResponseBody
    @RequestMapping("/addUserRole")
    public DataResults addUserRole(Integer roleId, String[] userIds){
        try {
            userRoleService.addUserRole(roleId,userIds);
            return DataResults.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResults.success(ResultCode.FAIL);
        }
    }

    /**
     * 修改角色对应的菜单
     * @param roleId
     * @param pageIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/updatePageRoleByRoleId")
    public DataResults updatePageRoleByRoleId(Integer roleId, Integer[] pageIds){
        try {
            iRolePageService.updateRolePage(roleId,pageIds);
            return DataResults.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResults.success(ResultCode.FAIL);
        }
    }

}
